package com.manuelvs.bobbysadventure;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.handler.IUpdateHandler;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.Scene.IOnSceneTouchListener;
import org.anddev.andengine.entity.scene.background.SpriteBackground;
import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

public class oneplayer extends BaseGameActivity implements IUpdateHandler, IOnSceneTouchListener
{
	private Camera mCamera;

    private BitmapTextureAtlas mBitmapTextureAtlas;
    private TiledTextureRegion bobbymovement;
    private TiledTextureRegion bobbymovement2;
    private TextureRegion Campo;

    private Scene mMainScene;

    AnimatedSprite sbobbymovement ;
    AnimatedSprite sbobbymovement2;
    private bobby _bazul;
    private bobby _brojo;
	float touchX;
	float touchY;
    
    boolean killstarted=false;
    boolean remotekillstarted=false;
    char counter=0;
    char remotecounter=0;

	@Override
	public Engine onLoadEngine()
	{
		mCamera = new Camera(0, 0, 480, 320);
		return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE,new RatioResolutionPolicy(480, 320), mCamera));
	}

	@Override
	public void onLoadResources()
	{
        BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		this.mBitmapTextureAtlas = new BitmapTextureAtlas(1024, 1024, TextureOptions.BILINEAR);
        this.bobbymovement = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "bobbysmovement.png", 0, 0, 6, 6);
        this.bobbymovement2 = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "bobbysmovementrojo.png", 301, 0, 6, 6);
        this.Campo = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "campo.png", 0, 361);
        this.mEngine.getTextureManager().loadTextures(this.mBitmapTextureAtlas);
	}

	@Override
	public Scene onLoadScene()
	{
	    mMainScene = new Scene();
		SpriteBackground bg = new SpriteBackground(new Sprite(0, 0, Campo));
		mMainScene.setBackground(bg);
		_bazul = new bobby();
		_brojo = new bobby();
		
		_brojo._Width=270;
		_brojo._height=149;
		_bazul._Width=130;
		_bazul._height=149;
		
	    sbobbymovement = new AnimatedSprite(0, 0, bobbymovement);
	    sbobbymovement.animate(new long[] {30}, new int[] {18}, 30);
	    mMainScene.attachChild(sbobbymovement);
	    sbobbymovement.setScale(0.5f);
	    
	    sbobbymovement2 = new AnimatedSprite(0, 0, bobbymovement2);
	    sbobbymovement2.animate(new long[] {30}, new int[] {18}, 30);
	    mMainScene.attachChild(sbobbymovement2);
	    sbobbymovement2.setScale(0.5f);

	    mMainScene.registerUpdateHandler(this);
	    mMainScene.setOnSceneTouchListener(this);

	    return mMainScene;
	}

	@Override
	public void onLoadComplete() {}

	private void movebobby(float pSecondsElapsed)
	{
		if(_bazul.isJumping()) _bazul.jump();
		if(_brojo.isJumping()) _brojo.jump();
		_bazul.move(pSecondsElapsed);
		_brojo.move(pSecondsElapsed);
		_brojo.operation=_brojo.movecpu(_bazul._Width, _bazul._height, _brojo._Width, _brojo._height);
		sbobbymovement.animate(new long[] {30}, new int[] {_bazul.animate}, 30);
		sbobbymovement.setPosition(_bazul._Width, _bazul._height);
		
		sbobbymovement2.animate(new long[] {30}, new int[] {_brojo.animate}, 30);
		sbobbymovement2.setPosition(_brojo._Width, _brojo._height);
		
		if((_bazul.kill && sbobbymovement2.collidesWith(sbobbymovement)) || killstarted)
		{
			counter++;
			if(counter>20)
			{
				killstarted=true;
				sbobbymovement2.setPosition(_brojo._Width, _brojo._height+11);
				sbobbymovement2.setRotation(90);
				sbobbymovement2.animate(new long[] {30}, new int[] {9}, 30);
			}
			if(counter==40)
			{
				counter=0;
				_brojo._Width=270;
				_brojo._height=149;
				_bazul.kill=false;
				_bazul.score++;
				sbobbymovement2.setRotation(0);
				killstarted=false;
			}
		}
		if(_bazul.kill && !sbobbymovement2.collidesWith(sbobbymovement))
		{
			_bazul.kill=false;
		}
		if((_brojo.kill && sbobbymovement2.collidesWith(sbobbymovement)) || remotekillstarted)
		{
			remotecounter++;
			if(remotecounter>20)
			{
				remotekillstarted=true;
				sbobbymovement.setPosition(_bazul._Width, _bazul._height+11);
				sbobbymovement.setRotation(90);
				sbobbymovement.animate(new long[] {30}, new int[] {9}, 30);
			}
			if(remotecounter==40)
			{
				remotecounter=0;
				_bazul._Width=130;
				_bazul._height=149;
				_brojo.kill=false;
				_brojo.score++;
				sbobbymovement.setRotation(0);
				remotekillstarted=false;
			}
		}
		if(_brojo.kill && !sbobbymovement2.collidesWith(sbobbymovement))
		{
			_brojo.kill=false;
		}
	}

    @Override
    public boolean onSceneTouchEvent(Scene scene, TouchEvent touchEvent)
    {
    	touchX=touchEvent.getX();
    	touchY=touchEvent.getY();
		  if (touchEvent.isActionDown())
		  {
	    	if(touchY>260&&touchX<420&&touchX>60)
	    		_bazul.operation=3;
	    	if(touchY>260 && touchX<60)
	    		_bazul.operation=8;
	    	if(touchY>260 && touchX>420)
	    		_bazul.operation=9;
	    	if(touchY<60&&touchX<420&&touchX>60)
	    		_bazul.operation=4;
	    	if(touchX<60&&touchY<260&&touchY>60)
	    		_bazul.operation=2;
	    	if(touchX>420&&touchY<260&&touchY>60)
	    		_bazul.operation=1;
	    	if((touchX>420)&&(touchY<60))
	    		_bazul.operation=5;
	    	if((touchX<60)&&(touchY<60))
	    		_bazul.operation=6;
	    	if(touchX>60 && touchX<420 && touchY>60 &&touchY<260)
	    		_bazul.operation=7;
		  }
		  
		if (touchEvent.isActionUp())
		{
			_bazul.operation=0;
		}
		return true;
    }

	@Override
	public void onUpdate(float pSecondsElapsed)
	{
		movebobby(pSecondsElapsed);
		Log.d("plataforma", Integer.toString(_brojo.plataforma));
		Log.d("rplataforma", Integer.toString(_brojo.remoteplataforma));
	}

	@Override
	public void reset() {}
	
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.more_apps:
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/developer?id=ManuelVS"));
        	startActivity(browserIntent);
            return true;
        case R.id.configuration:
        	final Intent configuration = new Intent(oneplayer.this, configuration.class);
        	startActivity(configuration);
            return true;
        }
        return false;
        
    }
}